<?php
namespace Admin\Controller;

class ActorController extends PublicController
{
    //角色列表
    public function index()
    {
        $list = M('actor')->select();
        $this->assign('list', $list);
        $this->display('Actor/index');
    }

    //执行分配角色
    public function doalloActor()
    {
        // v($_POST);exit;
        if (empty($_POST['user_id'])) {
            $this->error('请选择用户来分配角色');
        }
        if (IS_POST) {
            M('user_actor')->where('user_id='.$_POST['user_id'])->delete();
            foreach ($_POST['aname'] as $v) {
                $arr = array();
                $arr['user_id'] = $_POST['user_id'];
                $arr['actor_id'] = $v;
                // v($arr);exit;
                M('user_actor')->add($arr);
            }
            $this->success('分配角色成功', U('AdminUser/index'));
        } else {
            $this->error('禁止非法传值');
        }
    }

    //分配角色显示
    public function alloActor()
    {
        $uid = $_GET['id'];
        $uname = $_GET['name'];
        $list = M('actor')->where('status=1')->select();
        $this->assign('uname', $uname);
        $this->assign('uid', $uid);
        $this->assign('list', $list);
        $this->display('Actor/alloActor');
    }

    //分配权限显示
    public function alloIndex()
    {
        $id = $_GET['id'];
        $aname = $_GET['aname'];
        $list = M('role')->field('id,node')->select();
        $role = D('Actor')->relation(true)->where('id='.$id)->select();
        $m = 0;
        foreach ($role[0]['role'] as $val) {
            $role[$m] = $val['id'];
            $m++;
        }
        // v($role);exit;
        $this->assign('uid', $id);
        $this->assign('aname', $aname);
        $this->assign('list', $list);
        $this->assign('role', $role);
        // v($list);exit;
        $this->display('Actor/allocation');
    }

    //执行分配
    public function doallo()
    {
        // v($_POST);
        if (empty($_POST['id'])) {
            $this->error('请选择角色来分配权限');
        }
        if (IS_POST) {
            M('actor_role')->where('actor_id='.$_POST['id'])->delete();
            foreach ($_POST['node'] as $v) {
                $arr = array();
                $arr['actor_id'] = $_POST['id'];
                $arr['role_id'] = $v;
                // v($arr);exit;
                M('actor_role')->add($arr);
            }
            $this->success('分配权限成功', U('Actor/index'));
        } else {
            $this->error('禁止非法传值');
        }

    }

    //添加角色显示
    public function addActor()
    {
        $this->display('Actor/add');
    }

    //执行添加
    public function add()
    {
        if (IS_POST) {
            if (!D('Actor')->create()) {
                $this->error(D('Actor')->getError());
            } else {
                if (D('Actor')->add()) {
                    $this->success('添加成功',U('Actor/index'));
                } else {
                    $this->error('添加失败');
                }
            }
        } else {
            $this->error('禁止非法添加');
        }
    }

    //执行角色启用或禁用
    public function status()
    {
        //判断GET里有无id
        if (empty($_GET['id'])) {
            $this->redirect('Actor/index');
        }

        //过滤非法id值
        $id = I('get.id/d');
        $status = I('get.status/d');
        $status = ($status=='1')?'0':'1';
        if ($id <= 0) {
            $this->error('禁止传入非法数字或负数', U('Actor/index'));
        } else {
            M('actor')->where(array('id'=>$id))->save(array('status'=>$status));
            $this->redirect('Actor/index');
        }
    }

    //编辑角色
    public function edit()
    {
        //判断GET里有无id
        if (empty($_GET['id'])) {
            $this->redirect('Actor/index');
        }

        //过滤非法id值
        $id = I('get.id/d');
        $list = M('actor')->where('id='.$id)->select();
        $this->assign('id', $id);
        $this->assign('list', $list);
        $this->display('Actor/edit');
    }

    //执行编辑
    public function update()
    {
        $id = I('post.id/d');
        if ($id <= 0) {
            $this->error('id不能传入非法数字或负数');
        } else {
            if (!IS_POST) {
                $this->error('禁止非法传入数据');
            } else {
                $actor = D('actor');
                if (!$actor->create()) {
                    // v($_POST);exit;
                    $this->error($actor->getError());
                } else {
                    if ($actor->where('id='.$id)->save()) {
                        $this->success('编辑成功',U('Actor/index'));
                    } else {
                        $this->error('编辑失败');
                    }
                }
            }
        }
    }

    //执行删除
    public function del()
    {
        //判断GET中有无id值
        if (empty($_GET['id'])) {
            $this->redirect('Actor/index');
        }

        $id = I('get.id/d');
        if ($id <= 0) {
            $this->error('id不能传入非法数字或负数');
        } else {
            if (M('actor')->where('id='.$id)->delete($id)) {
                $this->success('删除成功');
            } else {
                $this->error('删除失败');
            }
        }
    }

}
